package com.ruoyi.warning.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.warning.domain.dto.PropertyDTO;
import com.ruoyi.warning.domain.entity.Configuration;
import com.ruoyi.warning.domain.entity.Property;
import com.ruoyi.warning.domain.query.PropertyQuery;
import com.ruoyi.warning.domain.vo.PropertyVO;
import com.ruoyi.warning.mapper.PropertyMapper;
import com.ruoyi.warning.service.IConfigurationService;
import com.ruoyi.warning.service.IPropertyService;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.IOException;
import java.text.ParseException;
import java.util.List;

/**
 * <p>
 * 资产表 服务实现类
 * </p>
 *
 * @author chenhc
 * @since 2025-05-14
 */
@Service
public class PropertyServiceImpl extends ServiceImpl<PropertyMapper, Property> implements IPropertyService {

    @Resource
    private PropertyMapper propertyMapper;
    @Resource
    private IConfigurationService configurationService;
    @Override
    public TableDataInfo getPage(PropertyQuery query) {
        Page<Property> page = PageHelper.startPage(query.getPageNum(),query.getPageSize());
        List<PropertyVO> list = propertyMapper.getList(query);
        return new TableDataInfo(list, page.getTotal());
    }

    @Override
    public void importExcel(MultipartFile file) throws IOException, ParseException {
        ExcelUtil<Property> util = new ExcelUtil<Property>(Property.class);
        List<Property> list = util.importExcel(file.getInputStream());
        // 定义输入日期的格式
//        SimpleDateFormat inputFormat = new SimpleDateFormat("EEE MMM dd HH:mm:ss z yyyy", java.util.Locale.ENGLISH);
        // 定义输出日期的格式
//        SimpleDateFormat outputFormat = new SimpleDateFormat("yyyy-MM-dd");
        String ipPattern = "^((25[0-5]|2[0-4]\\d|[01]?\\d\\d?)\\.){3}(25[0-5]|2[0-4]\\d|[01]?\\d\\d?)$";
        for (Property property : list) {
            if(ObjectUtils.isEmpty(property.getHostname())||!property.getHostname().matches(ipPattern)){
                list.remove( property);
                continue;
            }
            // 去除 hostname 字段中的 /t、/n 和空格
            if (ObjectUtils.isNotEmpty(property.getHostname())) {
                String hostname = property.getHostname();
                hostname = hostname.replaceAll("\n", "");
                hostname = hostname.replaceAll("\t", "");
                hostname = hostname.replaceAll("\\s", "");
                property.setHostname(hostname);
//                property.setHostname(property.getHostname().replaceAll("[\\s/tn]", ""));
            }
            if(ObjectUtils.isNotEmpty(property.getManufactureDate())){
//                Date date = inputFormat.parse(property.getManufactureDate());
//                property.setManufactureDate(outputFormat.format(date));
//                System.out.println(property.getManufactureDate().toString());
            }else {
                property.setManufactureDate(null);
            }
            if(ObjectUtils.isNotEmpty(property.getWarrantyDate())){
//                Date date = inputFormat.parse(property.getWarrantyDate());
//                property.setWarrantyDate(outputFormat.format(date));
//                System.out.println("质保"+property.getWarrantyDate().toString());
            }else {
                property.setWarrantyDate(null);
            }
            if(ObjectUtils.isEmpty(property.getPortsNums())){
                property.setPortsNums(0);
            }
        }
        propertyMapper.batchInsert(list);
//        this.saveBatch(list);
    }

    @Override
    public void collect(PropertyDTO propertyDTO) {
        //找到所有的配置
        List<Configuration> list = configurationService.list(new LambdaQueryWrapper<Configuration>()
                .in(Configuration::getId,propertyDTO.getConfigurationList()));
        for (Configuration configuration : list) {
            configuration.setDeviceId(configuration.getDeviceId()+","+propertyDTO.getDeviceId());
        }
        configurationService.updateBatchById(list);
    }
}
